package ldh.im.monitor.service;

import ldh.im.fxbase.util.ThreadPoolUtil;
import ldh.im.util.ConfigUtil;
import ldh.rpc.client.RpcClientBootstrap;
import ldh.rpc.client.RpcClientConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

public class ConnectionCheckService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ConnectionCheckService.class);

    private RpcClientBootstrap clientBootstrap = null;

    public ConnectionCheckService(RpcClientBootstrap clientBootstrap) {
        this.clientBootstrap = clientBootstrap;
    }

    public void reconnectionCheck() {
        ThreadPoolUtil.scheduleAtFixedRate(()->{
            if (!clientBootstrap.isActive()) {
                try {
                    ImCommunicateService imCommunicateService = new ImCommunicateService(clientBootstrap.getRpcConfig());
                    imCommunicateService.reconnection((RpcClientConnection)clientBootstrap.getRpcConfig().getRpcConnection());
                } catch (Exception e) {
                    LOGGER.error("重连失败", e);
                }
            }
        }, 1, 5, TimeUnit.SECONDS);
    }
}
